package handlers

import (
	"context"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
	"github.com/twwch/gin-sdk/handler"
	"github.com/twwch/gin-sdk/server"
	"net/http"
	serviceSvc "twwch/k8s-admin/application/service"
	"twwch/k8s-admin/models/common"
	"twwch/k8s-admin/models/service"
)

type ServiceHandler struct {
	handler.Base
	serviceSvc *serviceSvc.ServiceService
}

func (h *ServiceHandler) Init(ginRouter *gin.RouterGroup) {
	h.Logger = log.WithField("handler", "service")
	h.serviceSvc = serviceSvc.GetService()
	// registry http handler
	if ginRouter != nil {
		appGroup := ginRouter.Group("/svc")
		server.Route(appGroup, http.MethodPost, "/create", h.Create)
		server.Route(appGroup, http.MethodGet, "/delete", h.Delete)
		server.Route(appGroup, http.MethodGet, "/list", h.List)
		//appGroup.GET("/", warper.CreateHandlerFunc(h.Test, false))
	}
}

// curl 127.0.0.1:8081/v1/svc/create -X POST -d '{"name": "test", "namespace": "test", "type": "NodePort", "selector": {"app": "test-deploy"}, "ports": [{"port": 80, "targetPort": 80, "protocol": "TCP"}]}' -H 'Content-Type: application/json; charset=utf-8'
func (h *ServiceHandler) Create(ctx context.Context, req *service.CreateReq) (*service.CreateResp, error) {
	return h.serviceSvc.Create(ctx, req)
}

func (h *ServiceHandler) Delete(ctx context.Context, req *service.DeleteReq) (*common.Empty, error) {
	return h.serviceSvc.Delete(ctx, req)
}

func (h *ServiceHandler) List(ctx context.Context, req *service.ListReq) (*service.ListResp, error) {
	return h.serviceSvc.List(ctx, req)
}
